package com.mstarc.app.mnfc.okhttp;


import android.util.Log;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by qdyouke01 on 2017/7/3.
 */

public class OkHttpRequest {
    public final static int CONNECT_TIMEOUT = 60;
    public final static int READ_TIMEOUT = 100;
    public final static int WRITE_TIMEOUT = 60;
    private static final String TAG = "OkHttpRequest";
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private OkHttpClient client;
    private OKHttpResult okHttpResult;
    private ExecutorService executorService;
    private static OkHttpRequest okHttpRequest;

    public static OkHttpRequest getInstance(OKHttpResult okHttpResult) {
        if (okHttpRequest == null) {
            okHttpRequest = new OkHttpRequest();
            okHttpRequest.okHttpRequest.getClient();
            okHttpRequest.initExecutorService();
        }
        okHttpRequest.okHttpResult = okHttpResult;
        return okHttpRequest;
    }

    private void initExecutorService() {
        if (executorService == null) {
            executorService = Executors.newFixedThreadPool(5);
        }
    }

    private void getClient() {
        if (client == null)
            client = new OkHttpClient.Builder()
                    .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)//设置读取超时时间
                    .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)//设置写的超时时间
                    .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)//设置连接超时时间
                    .build();
    }

    public void jsonPost(String httpUrl, String params, int msgFlag) {
        executorService.execute(getJsonPostRunable(httpUrl, params, msgFlag));
    }

    public void getHttp(String httpUrl, int msgFlag) {
        executorService.execute(getGetRunable(httpUrl, msgFlag));
    }

    private Runnable getJsonPostRunable(final String httpUrl, final String params, final int msgFlag) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                RequestBody body = RequestBody.create(JSON, params);
                Request request = new Request.Builder()
                        .url(httpUrl)
                        .post(body)
                        .build();
                Response response;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        okHttpResult.requestJsonOnSucceed(response.body().string(), msgFlag);
                    } else {
                        okHttpResult.requestJsonOnError(msgFlag);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    okHttpResult.requestJsonOnLocalError(msgFlag);
                }
            }
        };
        return runnable;
    }

    private Runnable getGetRunable(final String httpUrl, final int msgFlag) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(httpUrl)
                        .build();
                Response response;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        okHttpResult.requestJsonOnSucceed(response.body().string(), msgFlag);
                    } else {
                        Log.d(TAG, "run: 1");
                        okHttpResult.requestJsonOnError(msgFlag);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d(TAG, "run: 2");
                    okHttpResult.requestJsonOnLocalError(msgFlag);
                }
            }
        };
        return runnable;
    }
}
